import os
import pickle

class ExecutionRecorder:
    def __init__(self, config, common_paths) -> None:
        self.config = config
        self.common_paths = common_paths
        self.execution_recorder_path = os.path.join(self.common_paths.get('meta-data-path'), 'execution_recorder.pkl')

    def save_script_record(self, zip_filename, values = {}):
        current_records = {}
        if os.path.exists(self.execution_recorder_path):
            with open(self.execution_recorder_path, 'rb') as f:
                current_records = pickle.load(f)
            current_records.update({zip_filename: values})
        with open(self.execution_recorder_path, 'wb') as f:
            pickle.dump(current_records, f)

    def load_script_record(self, zip_filename):
        try:
            with open(self.execution_recorder_path, 'rb') as f:
                values = pickle.load(f).get(zip_filename)
                module_name = values.get('module_name')
                if not os.path.exists(os.path.join(self.common_paths.get('fake-data-generator-path'), f'{module_name}.py')):
                    return None, None
                return module_name, values.get('data_type')
        except Exception:
            return None, None
